package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.goods.ManageSpecNameVO;
import com.yuanfeng.goods.dto.GdbMerchantGoodsSpecValueDTO;
import com.yuanfeng.goods.dto.ManageSpecValueDTO;
import com.yuanfeng.goods.dto.ManageSpecValueQueryDTO;
import com.yuanfeng.goods.entity.GoodsSpecValueEntity;
import com.yuanfeng.goods.mapper.GoodsSpecMapper;
import com.yuanfeng.goods.mapper.GoodsSpecValueMapper;
import com.yuanfeng.goods.service.GoodsSpecValueService;
import com.yuanfeng.goods.vo.ManageSpecValueResultVO;
import com.yuanfeng.goods.vo.ManageSpecValueVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsSpecValueServiceImpl
 * @Description 商品规格值
 * @Author cf
 * @Date 2023/6/27 17:31
 * @Version 1.0
 */
@Service
public class GoodsSpecValueServiceImpl extends ServiceImpl<GoodsSpecValueMapper, GoodsSpecValueEntity> implements GoodsSpecValueService {

    @Resource
    private GoodsSpecMapper goodsSpecMapper;

    @Override
    @Transactional
    public ResponseResult updateBySpecId(ManageSpecValueDTO param2) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_GOODS_SPEC, param2.getToken());
        if (userPermissionsResult.getFlag()) {
            Integer shopId = Integer.valueOf(userPermissionsResult.getShopId());
            List<GdbMerchantGoodsSpecValueDTO> params = param2.getSpecValue();
            // 获取更新的规格
            List<GdbMerchantGoodsSpecValueDTO> updateParam = new ArrayList<GdbMerchantGoodsSpecValueDTO>();
            // 获取新增的规格
            List<GdbMerchantGoodsSpecValueDTO> addParam = new ArrayList<GdbMerchantGoodsSpecValueDTO>();
            Integer specIdVar=0;
            for (GdbMerchantGoodsSpecValueDTO param : params) {
                if (param.getSpecId() == null) {
                    specIdVar=param.getSpecId();
                    List<ManageSpecNameVO> specvalue = goodsSpecMapper.selectByTypeId(param.getTypeId());
                    List<Integer> specIdList = new ArrayList<>();
                    for (ManageSpecNameVO varList : specvalue) {
                        specIdList.add(varList.getSpecId());
                        //???
                        param.setSpecId(varList.getSpecId());
                    }


                    if (null == param.getSpecValueDisplayorder()) {
                        param.setSpecValueDisplayorder(0);
                    }

                    if (param.getSpecValueId() != null) {
                        param.setShopId(shopId);
                        updateParam.add(param);
                    } else {
                        param.setShopId(shopId);
                        addParam.add(param);
                    }
                }else {
                    if (null == param.getSpecValueDisplayorder()) {
                        param.setSpecValueDisplayorder(0);
                    }
                    param.setShopId(shopId);
                    addParam.add(param);
                }
            }

            // 更新或新增规格
            if (addParam.size() > 0) {
                this.baseMapper.insertBatch(addParam);
            }
            if (updateParam.size() > 0) {
                this.baseMapper.updateBatch(updateParam);
            }
            //
        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryGoodsSpecByCatId(ManageSpecValueQueryDTO param) {
        Map<String, List<String>> resultSpecMap = new HashMap<>();

        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_GOODS,
                param.getToken());

        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        param.setShopId(userPermissionsResult.getShopId());
        ManageSpecValueVO manageSpecValueVO = new ManageSpecValueVO();
        manageSpecValueVO.setCatId(param.getCatId());
        manageSpecValueVO.setTypeId(param.getTypeId());
        // 查询规格值
        List<ManageSpecValueResultVO> specValueList = this.baseMapper.selectByShopId2(manageSpecValueVO);
        if (null != specValueList && !specValueList.isEmpty()) {
            for (ManageSpecValueResultVO result : specValueList) {
                if (resultSpecMap.containsKey("specName")) {
                    List<String> value = resultSpecMap.get(result.getSpecName());
                    value.add(result.getSpecValueName());
                    resultSpecMap.put(result.getSpecName(), value);
                } else {
                    List<String> value = new ArrayList<>();
                    value.add(result.getSpecValueName());
                    resultSpecMap.put(result.getSpecName(), value);
                }
            }
        }
        return ResponseResult.success(resultSpecMap);
    }

    @Override
    public ResponseResult queryShopSpecs(ManageSpecValueQueryDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_GOODS_SPEC, param.getToken());

        if (userPermissionsResult.getFlag()) {
            param.setShopId(userPermissionsResult.getShopId());
            // 规格列表
            ManageSpecValueVO manageSpecValueVO = new ManageSpecValueVO();
            manageSpecValueVO.setCatId(param.getCatId());
            manageSpecValueVO.setTypeId(param.getTypeId());
            List<ManageSpecValueResultVO> specValueList = this.baseMapper.selectByShopId2(manageSpecValueVO);
            return ResponseResult.success(specValueList);
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }

    }


}
